package com.jeesite.modules.fl.web.app;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.mapper.JsonMapper;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.fl.dto.EvaluateDto;
import com.jeesite.modules.fl.entity.Appointment;
import com.jeesite.modules.fl.entity.Evaluate;
import com.jeesite.modules.fl.entity.EvaluateDetail;
import com.jeesite.modules.fl.entity.Leaguer;
import com.jeesite.modules.fl.service.*;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@Controller
public class AppEvaluateController extends BaseController {
    @Autowired
    private EvaluateService evaluateService;
    @Autowired
    private EvaluateDetailService evaluateDetailService;
    @Autowired
    private LeaguerService leaguerService;
    @Autowired
    private AppointmentService appointmentService;
    @Autowired
    private ClinicService clinicService;
    @Autowired
    private DoctorService doctorService;
    /**
     * 查询评价列表数据
     */
    @RequestMapping(value = "/evaluate/listData")
    @ResponseBody
    @RequiresUser
    public Page<Appointment> listData(Appointment appointment, HttpServletRequest request, HttpServletResponse response) {
        appointment.setIsEvaluate("1");
        appointment.setStatus("0");
        appointment.setCreateBy(UserUtils.getUser().getUserCode());
        Page<Appointment> page = new Page<Appointment>(request, response);
        appointment.setPage(page);
        page.setList(appointmentService.getEvaluates(appointment));
        return page;
    }


    @PostMapping(value = "/evaluate/saveScore")
    @ResponseBody
    @RequiresUser
    public String saveScore(@Validated Evaluate evaluate) {
        evaluateService.save(evaluate);
        return renderResult(Global.TRUE, text("保存评价成功！"));
    }


    @PostMapping(value = "/evaluate/saveDetail")
    @ResponseBody
    @RequiresUser
    public String saveDetail(@Validated EvaluateDetail evaluateDetail) {
        evaluateDetail.setSource("1");
        evaluateDetailService.save(evaluateDetail);
        return renderResult(Global.TRUE, text("保存评价明细成功！"));
    }

    @RequestMapping(value = "/appointment/{appointmentId}/saveEvaluate", method = RequestMethod.POST)
    @ResponseBody
    @RequiresUser
    public String save(HttpServletRequest request, @PathVariable String appointmentId, String data) throws IOException {
        EvaluateDto evaluateDto = JsonMapper.fromJson(data,EvaluateDto.class);
        appointmentService.saveEvaluate(appointmentId,evaluateDto.getScores(),evaluateDto.getDetail());
        return renderResult(Global.TRUE, text("保存评价成功！"));
    }

    @RequestMapping(value = "/evaluate/getMyList")
    @ResponseBody
    @RequiresUser
    public Page<Appointment> getMyList(HttpServletRequest request, HttpServletResponse response,String lang) {
        User user = UserUtils.getUser();
        Leaguer leaguer = leaguerService.get(user.getRefCode());
        Appointment entity = new Appointment();
        entity.setStatus("0");
        entity.setLeaguer(leaguer);
        entity.setIsEvaluate("1");
        Page<Appointment> page = appointmentService.findPage(new Page<Appointment>(request, response), entity);
        for(Appointment appointment: page.getList()){
            appointmentService.getEvaluate(appointment);
            if(lang != null && "2".equals(lang)) {
                clinicService.setEngLang(appointment.getClinic());
                doctorService.setEngLang(appointment.getDoctor());
            }
        }
        appointmentService.updateEvaluateDetailRead(entity);
        return page;
    }
}
